package com.apes.framework.plugin.workflow.lib;

import com.apes.framework.plugin.workflow.parser.model.Node;
import com.apes.framework.util.Tools;
import com.apestech.api.workflow.WorkFlow;
import com.apestech.api.workflow.lib.element.FlowTask;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringUtil;
import org.flowable.engine.ProcessEngine;
import org.flowable.task.api.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 提交
 *
 * @author hup
 * @create 2018-07-24-9:43
 */
public class WorkFlowExecute extends OAUtil{

    private ProcessEngine engine;
    private WorkFlow workFlow;
    private List<String> users;

    public WorkFlowExecute(WorkFlow workFlow){
        this.workFlow=workFlow;
        this.engine=workFlow.getEngine();
    }

    public List<String> getUsers() {
        return users;
    }

    public void execute(Task task, Node node, Map mapping, Map webmap, JSONObject input, FlowTask flowTask) throws Exception {
        users = new ArrayList<>();
        if (!node.isAutomatic()) {
            Object taskExecutor = Tools.isNull(mapping.get("taskExecutor")) ? webmap.get("taskExecutor") : mapping.get("taskExecutor");
            if (Tools.isNull(taskExecutor)) {
                throw new RuntimeException("任务执行人不允许为空！");
            }
            mapping.put("taskExecutor", taskExecutor); //最后执行人
            mapping.put("taskExecutorDepartment", Tools.isNull(mapping.get("taskExecutorDepartment")) ? webmap.get("taskExecutorDepartment") : mapping.get("taskExecutorDepartment")); //最后执行部门
            mapping.put("taskExecutorPost", Tools.isNull(mapping.get("taskExecutorPost")) ? webmap.get("taskExecutorPost") : mapping.get("taskExecutorPost")); //最后执行岗位

            /*if (Tools.isNull(task.getAssignee())) {
                users.addAll(OAUtil.getTaskCandidate(engine, task.getId()));
                input.putAll(webmap);
                input.put("userId",String.valueOf(taskExecutor));
                workFlow.claimUser(input); //任务签收
            } else {
                String assignee = OAUtil.getTaskAssignee(task);
                users.add(assignee);
            }*/
            users = getTaskUser(engine, task);
            if(!StringUtil.isNotEmpty(task.getAssignee())) {
                loginCliam(task, input, flowTask);
            }
        } else {
            String u=input.getString("loginUserId");
            input.put("userId", u);
            input.put("taskId", task.getId());
            workFlow.assignTaskToUser(task.getId(), u);
            flowTask.setState("assgin");
            workFlow.claimUser(input); //任务签收
        }
    }

    private void  loginCliam(Task task, JSONObject input, FlowTask flowTask) throws Exception {
        String u=input.getString("loginUserId");
        users.add(u);
        input.put("userId", u);
        input.put("taskId", task.getId());
        workFlow.assignTaskToUser(task.getId(), u);
        flowTask.setState("assgin");
        workFlow.claimUser(input); //任务签收
    }


}
